package DAO.Impl;

import A_Druid.DruidUtils;
import Bean.Emp;
import DAO.empDAO;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;
import java.util.Map;

public class empDAOImpl implements empDAO {
    private JdbcTemplate template = new JdbcTemplate(DruidUtils.ds);

    @Override
    public void updateSal(Emp emp) {
        String sql ="update emp set sal =10000 where empno = ?";
        template.update(sql,emp.getEmpno());
    }

    @Override
    public void insertEmp(Emp emp) {
        String sql ="insert into emp(empon,ename,sal,deptno)values(?,?,?,?)";
        template.update(sql,emp.getEmpno(),emp.getEname(),emp.getSal(),emp.getDeptno());
    }

    @Override
    public void deleteEmp(int empno) {
        String sql = "delete from emp where empno =?";
        template.update(sql, empno);
    }

    @Override
    public Emp selectByEmpno(int empno) {
        String sql = "select * from emp where empno = ?";
        List<Emp> list = template.query(sql, new BeanPropertyRowMapper<Emp>(Emp.class), empno);
        if (list == null) {
            System.out.println("错误员工号");
            return null;
        } else {
            return list.get(0);
        }
    }
    @Override
    public List selectALL() {
        String sql = "select * from emp ";
        List<Map<String,Object>> list = template.queryForList(sql);
        for(Map map:list) {
            System.out.println(map);
        }
        return list;
    }

    @Override
    public List selectEmpALL() {
        String sql = "select * from emp";
        List<Emp> list = template.query(sql,new BeanPropertyRowMapper<Emp>(Emp.class));
        for(Emp emp:list) {
            System.out.println(emp);
        }
        return list;
    }
}
